import { Component, OnInit, OnDestroy } from '@angular/core'
import { NzMessageService } from 'ng-zorro-antd'
import { Subject } from 'rxjs'

import { INewPageColumn } from '@shared/interfaces/common'
import { AcquirerNewBaseComponent } from './base/base.component'
import { AcquirerNewTradeComponent } from './trade/trade.component'
import { AcquirerNewQrcodeChannelComponent } from './qrcode-channel/qrcode-channel.component'
import { AcquirerNewCardChannelComponent } from './card-channel/card-channel.component'
import { AcquirerNewEcChannelComponent } from './ec-channel/ec-channel.component'
import { AcquirerNewUserComponent } from './user/user.component'
import { AcquirerNewSettlementComponent } from './settlement/settlement.component'
import { AcquirerNewQrcodeFuncComponent } from './qrcode-func/qrcode-func.component'
import { AcquirerNewCardFuncComponent } from './card-func/card-func.component'
import { ComWindow } from '@shared/classes/ComWindowClass'
import { AcquirerStoreService } from './acquirer-store.service'
import { AcquirerService } from '../acquirer.service'
import { Router, ActivatedRoute } from '@angular/router'
import { AcquirerNewAuditComponent } from './audit/audit.component'
import { PageService } from '@shared/services/page.service'
import { UserReq, User } from '@shared/interfaces/user'
import { encryptPassword } from 'app/utils/helpers'
import { ALPChannel, WXChannel, UPIChannel } from '@shared/interfaces/acquirer'
import { I18NService } from '@core'
import { AcquirerNewAcquirerFuncComponent } from './acquirer-func/acquirer-func.component'

@Component({
  selector: 'app-acquirer-new',
  templateUrl: './new.component.html',
})
export class AcquirerNewComponent implements OnInit, OnDestroy {
  pages: INewPageColumn[] = []

  valid = new Subject<boolean>()

  constructor(
    public msgSrv: NzMessageService,
    public router: Router,
    public route: ActivatedRoute,
    public pageService: PageService,
    private acquirerStoreService: AcquirerStoreService,
    private acquirerService: AcquirerService,
    private i18n: I18NService,
  ) {}

  ngOnInit(): void {
    this.initColumns()
  }

  ngOnDestroy(): void {
    this.acquirerStoreService.reset()
  }

  initColumns(): void {
    this.pages = [
      {
        component: new ComWindow(AcquirerNewBaseComponent, { title: 'GENERAL.INFO.OTHER.BASIC_CONFIG' }),
      },
      {
        component: new ComWindow(AcquirerNewTradeComponent, { title: 'GENERAL.INFO.OTHER.TRADE_INFO' }),
      },
      {
        component: new ComWindow(AcquirerNewQrcodeChannelComponent, { title: 'BIZ.ACQUIRER.OTHER.QR_CHANNELS' }),
      },
      {
        component: new ComWindow(AcquirerNewCardChannelComponent, { title: 'BIZ.ACQUIRER.OTHER.CARD_CHANNELS' }),
      },
      {
        component: new ComWindow(AcquirerNewEcChannelComponent, { title: 'BIZ.ACQUIRER.OTHER.EC_CHANNELS' }),
      },
      {
        component: new ComWindow(AcquirerNewSettlementComponent, { title: 'GENERAL.INFO.OTHER.SETTLEMENT' }),
      },
      {
        component: new ComWindow(AcquirerNewAcquirerFuncComponent, { title: 'GENERAL.INFO.OTHER.ACQUIRER_FUNC_CONFIG'}),
      },
      // {
      //   component: new ComWindow(AcquirerNewQrcodeFuncComponent, { title: '扫码功能配置' }),
      // },
      // {
      //   component: new ComWindow(AcquirerNewCardFuncComponent, { title: '刷卡功能配置' }),
      // },
      {
        component: new ComWindow(AcquirerNewAuditComponent, { title: 'GENERAL.INFO.OTHER.AUDIT_CONFIG'}),
      },
      {
        component: new ComWindow(AcquirerNewUserComponent, { title: 'GENERAL.INFO.OTHER.USERS' }),
      },
    ]
  }

  submit(): void {
    const acquirer = this.acquirerStoreService.value
    // encrypt user password
    const users = acquirer.users || []
    users.forEach((user: User) => {
      user.password = encryptPassword(user.password)
    })

    // encrypt signKey
    const channels = acquirer.qrChannels
    channels.forEach((channel: ALPChannel | WXChannel | UPIChannel) => {
      if (channel.signKey) {
        channel.signKey = encryptPassword(channel.signKey)
      }
    })

    this.acquirerService.create(acquirer).subscribe(acq => {
      /** 成功后不需要路由守卫 */
      this.pageService.triggerGuardAlive(false)
      this.msgSrv.success(this.trans('GENERAL.NOTIFICATION.CREATE_SUCCESS'))
      this.router.navigateByUrl('/biz/acquirers')
    })
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
